/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.insta.calendarMicroBlogage;

import processing.core.*;
import processing.core.PImage;
import remixlab.dandelion.core.InteractiveFrame;
import remixlab.dandelion.geom.Vec;
import remixlab.proscene.Scene;

/**
 *
 * @author liban
 */
public class SphereImage extends PApplet{

 private Scene scene;   
 private int ptsW, ptsH;
 private PImage Img ;
 private int numPointsW;
 private int numPointsH_2pi; 
 private int numPointsH;
 private float[] coorX;
 private float[] coorY;
 private float[] coorZ;
 private float[] multXZ;
 InteractiveFrame iFrame;
 Blogage blogageConnection;

 
 // constructeur
 public SphereImage(){}
 public SphereImage(Scene scn)
 {
     this.scene = scn;    
    iFrame = new InteractiveFrame(scene);
    iFrame.setGrabsInputThreshold(25);
 }
 
 // Getters && Setters
    
    // 
   

    public Scene getScene() {
        return scene;
    }

    public void setScene(Scene scene) {
        this.scene = scene;
    }

    public Blogage getBlogageConnection() {
        return blogageConnection;
    }

    public void setBlogageConnection(Blogage blogageConnection) {
        this.blogageConnection = blogageConnection;
    }
 
    public int getPtsW() {
        return ptsW;
    }

    public void setPtsW(int ptsW) {
        this.ptsW = ptsW;
    }

    public int getPtsH() {
        return ptsH;
    }

    public void setPtsH(int ptsH) {
        this.ptsH = ptsH;
    }

    public PImage getImg() {
        return (Img);
    }

    public void setImg(String AdresseImage) {
        this.Img = loadImage(AdresseImage);
    }

    public int getNumPointsW() {
        return numPointsW;
    }

    public void setNumPointsW(int numPointsW) {
        this.numPointsW = numPointsW;
    }

    public int getNumPointsH_2pi() {
        return numPointsH_2pi;
    }

    public void setNumPointsH_2pi(int numPointsH_2pi) {
        this.numPointsH_2pi = numPointsH_2pi;
    }

    public int getNumPointsH() {
        return numPointsH;
    }

    public void setNumPointsH(int numPointsH) {
        this.numPointsH = numPointsH;
    }

    public float[] getCoorX() {
        return coorX;
    }

    public void setCoorX(float[] coorX) {
        this.coorX = coorX;
    }

    public float[] getCoorY() {
        return coorY;
    }

    public void setCoorY(float[] coorY) {
        this.coorY = coorY;
    }

    public float[] getCoorZ() {
        return coorZ;
    }

    public void setCoorZ(float[] coorZ) {
        this.coorZ = coorZ;
    }

    public float[] getMultXZ() {
        return multXZ;
    }

    public void setMultXZ(float[] multXZ) {
        this.multXZ = multXZ;
    }
    
//Methode 
    public void initializeSphere(int numPtsW, int numPtsH_2pi) {

      // The number of points around the width and height
      numPointsW=numPtsW+1;
      numPointsH_2pi=numPtsH_2pi; 
      numPointsH=ceil((float)numPointsH_2pi/2)+1;  

      coorX=new float[numPointsW];   // All the x-coor in a horizontal circle radius 1
      coorY=new float[numPointsH];   // All the y-coor in a vertical circle radius 1
      coorZ=new float[numPointsW];   // All the z-coor in a horizontal circle radius 1
      multXZ=new float[numPointsH];  // The radius of each horizontal circle (that you will multiply with coorX and coorZ)

      for (int i=0; i<numPointsW ;i++) {  // For all the points around the width
        float thetaW=i*2*PI/(numPointsW-1);
        coorX[i]=sin(thetaW);
        coorZ[i]=cos(thetaW);
      }

      for (int i=0; i<numPointsH; i++) 
      {  // For all points from top to bottom
        if((numPointsH_2pi/2) != numPointsH_2pi/2)
        {
          if (i==numPointsH-1) 
          {  // If the numPointsH_2pi is odd and it is at the last pt
            float thetaH=(i-1)*2*PI/(numPointsH_2pi);
            coorY[i]=cos(PI+thetaH); 
            multXZ[i]=0;
          }
      }
        else 
        {
          //The numPointsH_2pi and 2 below allows there to be a flat bottom if the numPointsH is odd
          float thetaH=i*2*PI/(numPointsH_2pi);

          //PI+ below makes the top always the point instead of the bottom.
          coorY[i]=cos(PI+thetaH); 
          multXZ[i]=sin(thetaH);
        }
      }
    }

    /*public void textureDeSphere(float rx, float ry, float rz, Blogage Bogageimage){ 

      // These are so we can map certain parts of the image on to the shape 
        setBlogageConnection(Bogageimage);
      //setImg("Bogageimage");
        //System.out.println("sedf"+ Bogageimage.getName());
        this.image(blogageConnection.getImage(),100,100);
     /* float changeU=this.getImg().width/(float)(getNumPointsW()-1); 
      float changeV=this.getImg().height/(float)(getNumPointsH()-1); 
      float u=0;  // Width variable for the texture
      float v=0;  // Height variable for the texture 
      beginShape(TRIANGLE_STRIP);
        texture(this.Img);
      for (int i=0; i<(numPointsH-1); i++) {  // For all the rings but top and bottom
        // Goes into the array here instead of loop to save time
        float coory=coorY[i];
        float cooryPlus=coorY[i+1];

        float multxz=multXZ[i];
        float multxzPlus=multXZ[i+1];

        for (int j=0; j<numPointsW; j++) {  // For all the pts in the ring
          normal(coorX[j]*multxz, coory, coorZ[j]*multxz);
          vertex(coorX[j]*multxz*rx, coory*ry, coorZ[j]*multxz*rz, u, v);
          normal(coorX[j]*multxzPlus, cooryPlus, coorZ[j]*multxzPlus);
          vertex(coorX[j]*multxzPlus*rx, cooryPlus*ry, coorZ[j]*multxzPlus*rz, u, v+changeV);
          u+=changeU;
        }
        v+=changeV;
        u=0;
      }
      endShape();
    }*/
}
